package com.sqfw.project.wp.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.alibaba.excel.Empty;
import com.sqfw.common.utils.DateUtils;
import com.sqfw.common.utils.StringUtils;
import com.sqfw.project.system.domain.SysDept;
import com.sqfw.project.system.domain.SysUser;
import com.sqfw.project.system.mapper.SysDeptMapper;
import com.sqfw.project.system.mapper.SysUserMapper;
import com.sqfw.project.wp.domain.*;
import com.sqfw.project.wp.domain.vo.ExportWpInfo;
import com.sqfw.project.wp.mapper.*;
import com.sqfw.project.wp.service.ISysCodeService;
import com.sqfw.project.wp.utils.ListUtils;
import com.sqfw.project.wpReport.domain.WpInfoTransfer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sqfw.project.wp.service.IWpHandleLogService;

/**
 * 工单主轨迹表Service业务层处理
 *
 * @author lx
 * @date 2023-06-07
 */
@Service
public class WpHandleLogServiceImpl implements IWpHandleLogService {
    @Autowired
    private WpHandleLogMapper wpHandleLogMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private WpInfoMapper wpInfoMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private WpVisitInfoMapper wpVisitInfoMapper;

    @Autowired
    private SysCodeMapper sysCodeMapper;

    @Autowired
    private ISysCodeService sysCodeService;


    /**
     * 查询工单主轨迹表列表
     *
     * @param wpHandleLog 工单主轨迹表
     * @return 工单主轨迹表
     */
    @Override
    public List<WpHandleLog> selectWpHandleLogList(WpHandleLog wpHandleLog) {
        return wpHandleLogMapper.selectWpHandleLogList(wpHandleLog);
    }

    /**
     * 新增工单主轨迹表
     *
     * @param wpHandleLog 工单主轨迹表
     * @return 结果
     */
    @Override
    public int insertWpHandleLog(WpHandleLog wpHandleLog) {
        String processId = wpHandleLogMapper.getWpHandleLogId();
        wpHandleLog.setProcessId("SQ" + processId);
        return wpHandleLogMapper.insertWpHandleLog(wpHandleLog);
    }

    /**
     * 修改工单主轨迹表
     *
     * @param wpHandleLog 工单主轨迹表
     * @return 结果
     */
    @Override
    public int updateWpHandleLog(WpHandleLog wpHandleLog) {
        wpHandleLog.setUpdateTime(DateUtils.getNowDate());
        return wpHandleLogMapper.updateWpHandleLog(wpHandleLog);
    }

    /**
     * 批量删除工单主轨迹表
     *
     * @param wpids 需要删除的工单主轨迹表主键
     * @return 结果
     */
    @Override
    public int deleteWpHandleLogByWpids(String[] wpids) {
        return wpHandleLogMapper.deleteWpHandleLogByWpids(wpids);
    }

    /**
     * 删除工单主轨迹表信息
     *
     * @param wpid 工单主轨迹表主键
     * @return 结果
     */
    @Override
    public int deleteWpHandleLogByWpid(String wpid) {
        return wpHandleLogMapper.deleteWpHandleLogByWpid(wpid);
    }

    @Override
    public WpHandleLog getWpHandleDetailedById(String processId) {

        WpHandleLog wpHandleLog = wpHandleLogMapper.selectWpHandleLogByProcessId(processId);
        String department = wpHandleLog.getDepartment();

        SysDept sysDept = sysDeptMapper.selectDeptById(Long.valueOf(department));
        if (sysDept != null) {
            wpHandleLog.setDepartmentName(sysDept.getDeptName());
        }
        if (wpHandleLog.getNextDepartment1() != null) {
            SysDept sysDeptCon = sysDeptMapper.selectDeptById(Long.valueOf(wpHandleLog.getNextDepartment1()));
            if (sysDeptCon != null) {
                wpHandleLog.setNextDepartmentName(sysDeptCon.getDeptName());
            }
        }

        WpInfo wpInfo = wpInfoMapper.selectWpInfoByWpId(wpHandleLog.getWpid());
        if (wpInfo != null) {
            wpHandleLog.setTimelimite(wpInfo.getTimelimite());
        }

        SysUser sysUser = sysUserMapper.selectUserByCode(wpHandleLog.getOperator());
        if (sysUser != null) {
            wpHandleLog.setNickName(sysUser.getNickName());
        }


        return wpHandleLog;
    }

    @Override
    public List<WpHandleLog> selectWpHandleLogListByDeptIds(WpInfoTransfer wpInfoTransfer) {
        return wpHandleLogMapper.selectWpHandleLogListByDeptIds(wpInfoTransfer);
    }

    @Override
    public List<ExportWpInfo> exportWpInfos(WpApplyItem item) {

        //查询承办单位工单数据
        List<String> wpids = wpHandleLogMapper.selectWpidsByDeptId(item);

        List<List<String>> lists = ListUtils.split(wpids, 1000);

        if (lists == null) {
            return new ArrayList<ExportWpInfo>();
        }
        List<ExportWpInfo> exportWpInfos = new ArrayList<>();

        WpHandleLog wpHandleLog = new WpHandleLog();

        List<SysCode> class1Codes = sysCodeService.selectSysCodeClass1List("class1", null);

        List<Long> class2pids = class1Codes.stream().map(SysCode::getAutoId).collect(Collectors.toList());
        List<SysCode> class2Codes = sysCodeService.selectSysCodeClass1List("class2", class2pids);

        List<Long> class3pids = class2Codes.stream().map(SysCode::getAutoId).collect(Collectors.toList());
        List<SysCode> class3Codes = sysCodeService.selectSysCodeClass1List("class3", class3pids);


        List<Long> class4pids = class3Codes.stream().map(SysCode::getAutoId).collect(Collectors.toList());
        List<SysCode> class4Codes = sysCodeService.selectSysCodeClass1List("class4", class4pids);



        for (List<String> wpidsCon : lists) {

            WpInfo wpInfo = new WpInfo();
            wpInfo.setWpids(wpidsCon);
            List<ExportWpInfo> exportWpInfoList = wpInfoMapper.exportWpInfos(wpInfo);

            for (ExportWpInfo exportWpInfo : exportWpInfoList) {
                String deptLevel2 = exportWpInfo.getDeptLevel2();
                wpHandleLog.setWpid(exportWpInfo.getWpid());
                wpHandleLog.setNextDepartment1(deptLevel2);
                wpHandleLog.setNextState("承办单位处理");
                //转承办单位处理时间
               wpHandleLogMapper.selectWpHandleLogList(wpHandleLog).stream()
                        .max(Comparator.comparing(WpHandleLog::getUpdateTime))
                       .ifPresent(log->exportWpInfo.setStarttime(log.getUpdateTime()));

                wpHandleLog.setNextState("工单回访");
                wpHandleLog.setNextDepartment1(null);
                //承办单位完结时间


                List<WpHandleLog> wpHandleLogList = wpHandleLogMapper.selectWpHandleLogListByUpdateTimeAsc(wpHandleLog).stream()
                        .filter(handleLogCon -> !"质检审核处理".equals(handleLogCon.getLastState()))
                        .collect(Collectors.toList());
                for (WpHandleLog handleLog : wpHandleLogList) {
                    if (handleLog.getUpdateTime().compareTo(exportWpInfo.getStarttime())>0){
                        exportWpInfo.setEndTime(handleLog.getUpdateTime());
                        exportWpInfo.setMessage(handleLog.getMessage());
                        break;
                    }
                }

                if (StringUtils.isNotEmpty(exportWpInfo.getClass1())) {
                    class1Codes.stream().filter(sysCode -> sysCode.getAutoId().toString().equals(exportWpInfo.getClass1()))
                            .findAny().ifPresent(code -> exportWpInfo.setClass1Name(code.getNodename()));
                }

                if (StringUtils.isNotEmpty(exportWpInfo.getClass2())) {
                    class2Codes.stream().filter(sysCode -> sysCode.getAutoId().toString().equals(exportWpInfo.getClass2()))
                            .findAny().ifPresent(code -> {
                        exportWpInfo.setClass2Name(code.getNodename());
                    });
                }

                if (StringUtils.isNotEmpty(exportWpInfo.getClass3())) {
                    class3Codes.stream().filter(sysCode -> sysCode.getAutoId().toString().equals(exportWpInfo.getClass3()))
                            .findAny().ifPresent(code -> {
                        exportWpInfo.setClass3Name(code.getNodename());

                    });
                }

                if (StringUtils.isNotEmpty(exportWpInfo.getClass4())) {
                    class4Codes.stream().filter(sysCode -> sysCode.getAutoId().toString().equals(exportWpInfo.getClass4()))
                            .findAny().ifPresent(code -> exportWpInfo.setClass4Name(code.getNodename()));
                }

                /*if (StringUtils.isNotEmpty(exportWpInfo.getClass2())){
                    SysCode sysCode = sysCodeMapper.selectSysCodeByAutoId(Long.valueOf(exportWpInfo.getClass2()));
                    exportWpInfo.setClass2Name(sysCode.getNodename());
                }

                if (StringUtils.isNotEmpty(exportWpInfo.getClass3())){
                    SysCode sysCode = sysCodeMapper.selectSysCodeByAutoId(Long.valueOf(exportWpInfo.getClass3()));
                    exportWpInfo.setClass3Name(sysCode.getNodename());
                }

                if (StringUtils.isNotEmpty(exportWpInfo.getClass4())){
                    SysCode sysCode = sysCodeMapper.selectSysCodeByAutoId(Long.valueOf(exportWpInfo.getClass4()));
                    exportWpInfo.setClass4Name(sysCode.getNodename());
                }

                if (StringUtils.isNotEmpty(exportWpInfo.getDistrict())){
                    SysCode sysCode = sysCodeMapper.selectSysCodeByAutoId(Long.valueOf(exportWpInfo.getDistrict()));
                    exportWpInfo.setDistrictName(sysCode.getNodename());
                }*/

            }
            exportWpInfos.addAll(exportWpInfoList);

        }
        return exportWpInfos;
    }
}
